#!/bin/bash
# Install Redis for LNMP-2013
# 2013-12-30

# Install Redis Server
[ -f redis-2.8.3.tar.gz ] || wget http://download.redis.io/releases/redis-2.8.3.tar.gz
tar zxf redis-2.8.3.tar.gz
cd redis-2.8.3
make PREFIX=/usr/local/redis install
if [ ! -f /usr/local/redis/bin/redis-server ] ; then
    echo -e "\033[31;49;1mError: Redis Install Failure.\033[39;49;0m"
    exit 1
fi
ln -s /usr/local/redis/bin/redis* /usr/local/bin/
mkdir /usr/local/redis/etc
mkdir /usr/local/redis/data
cp redis.conf /usr/local/redis/etc/
sed -i 's/daemonize no/daemonize yes/g' /usr/local/redis/etc/redis.conf
sed -i '/# bind 127.0.0.1/a\bind 0.0.0.0' /usr/local/redis/etc/redis.conf
sed -i 's/^dir/#dir/g' /usr/local/redis/etc/redis.conf
sed -i '/^#dir/a\dir /usr/local/redis/data' /usr/local/redis/etc/redis.conf
if [ "$linux_type" = "rhel" ] ; then
   echo "/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf" >> /etc/rc.local
else
   sed -i '/exit 0/i\/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf' /etc/rc.local
fi
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
cd ..

# Install redis.so for PHP
unzip phpredis-master.zip
cd phpredis-master
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
echo "Moving redis.so to: /usr/local/php/lib/php/extensions/redis.so"
mv /usr/local/php/lib/php/extensions/no-debug*/redis.so /usr/local/php/lib/php/extensions/
cd ..

cat >> /usr/local/php/etc/php.d/redis.ini << EOF
[php redis]
extension="/usr/local/php/lib/php/extensions/redis.so"
EOF

# Install phpRedisAdmin
tar zxf phpredisadmin-20131202.tar.gz
mv phpredisadmin /var/www/localhost/
chown www.www -R /var/www/localhost/phpredisadmin
cp redis.php /var/www/localhost/

if [ -f /etc/init.d/php-fpm ] ; then
   /etc/init.d/php-fpm restart
   [ "$?" = "0" ] || echo "Please manually restart PHP-FPM."
elif [ -f /etc/init.d/httpd ] ; then
   /etc/init.d/httpd restart
   [ "$?" = "0" ] || echo "Please manually restart Apache."
else
   echo "Please manually restart Apache or PHP-FPM."
fi
